{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#　本代码尝试读取预训练的字向量，查看其结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import struct\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_pretrained_embedding(path):\n",
    "    \"\"\"\n",
    "    读取token_vec_300.bin中训练好的词向量\n",
    "    返回为字典格式：，key为字，value为其300维的向量（array类型，每个数字为float32），\n",
    "    \"\"\"\n",
    "    embeddings_dict = {}\n",
    "    with open(path, 'r', encoding='utf-8') as f:\n",
    "        for line in f:\n",
    "            values = line.strip().split(' ')\n",
    "            if len(values) < 300:\n",
    "                continue\n",
    "            word = values[0]\n",
    "            coefs = np.asarray(values[1:], dtype='float32')\n",
    "            embeddings_dict[word] = coefs\n",
    "    print('Found %s word vectors.' % len(embeddings_dict))\n",
    "    return embeddings_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_embedding_matrix(path):\n",
    "    embedding_dict = load_pretrained_embedding(path)\n",
    "    # 假设存在 word_dict\n",
    "    word_dict = {'字': 0, '成': 1, '压': 2}\n",
    "    vocab_size = len(word_dict)\n",
    "    embedding_matrix = np.zeros((vocab_size + 1, 300))  # 预先生成（词表长度+1，词向量维度）的 embedding_matrix\n",
    "    for word, i in word_dict.items():\n",
    "        embedding_vector = embedding_dict.get(word)\n",
    "        if embedding_vector is not None:\n",
    "            # 如果词表中的词在embedding_dict中是有出现的，则将这个词的对应的词向量添加进embedding_matrix；不出现则表示为0\n",
    "            embedding_matrix[i] = embedding_vector\n",
    "    return embedding_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 20028 word vectors.\n",
      "(4, 300)\n",
      "[[-2.21914673  2.0009923  -3.91052651 ...  2.20963526  2.22798276\n",
      "  -2.82870269]\n",
      " [ 1.58638132  3.88313222  2.33893275 ... -1.1115495  -1.92947423\n",
      "   2.99110317]\n",
      " [-2.66316319 -1.64525831 -0.52681863 ...  0.23300435  2.60951686\n",
      "   2.22028923]\n",
      " [ 0.          0.          0.         ...  0.          0.\n",
      "   0.        ]]\n"
     ]
    }
   ],
   "source": [
    "path = './token_vec_300.bin'\n",
    "emb_mat = build_embedding_matrix(path)\n",
    "print(emb_mat.shape)\n",
    "print(emb_mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-3.74557161e+00,  6.62768245e-01, -2.24549747e+00, -4.26866341e+00,\n",
       "        9.18546319e-01, -1.47456182e-02, -2.97559023e+00, -2.09501410e+00,\n",
       "        3.44752431e+00, -3.96605897e+00,  1.42763972e+00,  2.85181642e+00,\n",
       "        5.46041787e-01,  1.88579547e+00, -4.22912025e+00,  6.38692021e-01,\n",
       "        2.24707603e+00,  5.57363570e-01,  3.47028852e-01,  1.04959095e+00,\n",
       "       -5.25265932e-01,  5.00741577e+00,  4.84664664e-02,  1.18904591e+00,\n",
       "        1.52522755e+00, -3.11517477e+00, -3.35766673e+00,  2.82229990e-01,\n",
       "       -4.50986147e+00, -2.12187982e+00,  2.49346882e-01, -1.07063174e+00,\n",
       "       -2.62602830e+00, -5.62154353e-01,  4.10935974e+00, -5.87283634e-02,\n",
       "        3.32595408e-01,  1.90404713e+00,  2.11565122e-02, -1.82761216e+00,\n",
       "       -1.48522675e+00, -9.08085942e-01, -4.10412073e+00, -2.97221828e+00,\n",
       "       -1.02699041e+00,  2.28860259e+00, -3.30522776e+00, -3.23394036e+00,\n",
       "       -3.63595724e+00,  2.15713215e+00,  1.83505073e-01, -2.64538199e-01,\n",
       "       -3.25654745e+00, -4.87543106e+00, -3.67698765e+00, -1.05164611e+00,\n",
       "       -5.16796303e+00,  2.55245709e+00,  7.67145157e+00, -1.61396548e-01,\n",
       "        2.06849647e+00,  3.94976795e-01,  9.91059244e-01, -1.06936860e+00,\n",
       "       -1.38097978e+00,  1.61893928e+00, -5.44272137e+00, -2.11091709e+00,\n",
       "        1.18868196e+00,  4.69447231e+00, -1.85220075e+00,  1.51499224e+00,\n",
       "       -1.71906841e+00,  1.56384766e+00, -1.40911674e+00, -1.62086642e+00,\n",
       "       -2.82027173e+00,  5.22939062e+00,  1.46519560e-02, -3.33506298e+00,\n",
       "        7.09902167e-01, -2.34090161e+00,  3.71930385e+00,  9.67651546e-01,\n",
       "        5.83811522e-01, -2.08925128e+00, -1.46424323e-01, -7.12175322e+00,\n",
       "        3.50097609e+00, -5.62380813e-02,  1.59403074e+00,  1.59993708e+00,\n",
       "        2.96768594e+00,  2.90104604e+00,  1.32892191e+00, -1.03070688e+00,\n",
       "       -1.60996962e+00,  5.96925616e-01,  2.27777338e+00,  2.23745346e+00,\n",
       "        3.14792705e+00,  4.36243200e+00,  2.93728399e+00,  6.90597668e-02,\n",
       "       -3.36956263e+00, -3.02678108e-01,  3.47563982e-01, -2.57157421e+00,\n",
       "       -2.98416162e+00,  1.57608950e+00,  5.24783754e+00, -2.29540658e+00,\n",
       "       -3.08714533e+00,  2.51292419e+00,  2.48269606e+00,  7.97159493e-01,\n",
       "        2.30124187e+00,  3.66940826e-01, -3.20532179e+00, -4.62959290e+00,\n",
       "       -3.60421324e+00, -1.17434168e+00,  5.46590185e+00, -1.70655632e+00,\n",
       "        1.59348083e+00,  1.83201230e+00, -2.55021381e+00, -2.73949838e+00,\n",
       "       -5.79947138e+00,  3.97110939e-01, -2.49451232e+00,  6.64903462e-01,\n",
       "        1.28804296e-01,  5.63880825e+00,  1.22353601e+00, -1.81241918e+00,\n",
       "        2.26427746e+00, -4.07191658e+00, -1.54202855e+00, -4.92032099e+00,\n",
       "       -1.78867030e+00,  1.17310688e-01,  3.30850315e+00, -1.24617422e+00,\n",
       "        1.50753105e+00,  3.65491652e+00,  2.41323042e+00,  5.72660303e+00,\n",
       "       -1.15338445e-01,  3.20524359e+00, -1.09684849e+00, -5.05639434e-01,\n",
       "       -1.74632728e+00,  1.28110141e-01, -1.71658683e+00, -1.21645415e+00,\n",
       "       -1.79818404e+00,  3.82675195e+00, -1.75662172e+00,  2.66683280e-01,\n",
       "        1.07748413e+00, -1.82341337e-01,  1.80453181e+00,  1.58210409e+00,\n",
       "        1.18270981e+00,  5.72557092e-01,  1.87160778e+00, -3.28286147e+00,\n",
       "       -5.38627958e+00, -4.31542218e-01, -2.49037433e+00, -3.96301746e+00,\n",
       "        1.88085091e+00, -2.03499413e+00, -3.31026614e-01, -7.07561791e-01,\n",
       "        2.95575404e+00, -2.68306398e+00,  1.19744110e+00,  2.30395341e+00,\n",
       "       -3.08409953e+00, -5.83901644e+00, -1.66425955e+00,  1.73397863e+00,\n",
       "       -3.65663201e-01,  3.13740253e+00,  1.97710764e+00,  2.40498304e+00,\n",
       "        2.65887141e+00, -2.76758456e+00,  2.19383550e+00, -2.24414952e-02,\n",
       "        1.93213153e+00, -2.11965370e+00, -7.63467252e-02,  8.40942919e-01,\n",
       "        2.04002142e+00,  3.95120049e+00, -2.00870252e+00, -3.63287139e+00,\n",
       "       -2.67364562e-01, -9.31428280e-03,  9.73472297e-01,  2.29016232e+00,\n",
       "       -7.74234533e-01, -3.00161686e-04, -1.90113449e+00,  2.73310041e+00,\n",
       "        1.79880545e-01, -6.72594786e+00,  1.13373004e-01, -4.61002827e+00,\n",
       "       -2.47651744e+00,  1.27083373e+00, -3.96437436e-01, -5.87023497e-01,\n",
       "       -2.55714178e+00,  3.64656657e-01,  2.18461680e+00, -3.53065562e+00,\n",
       "        4.08643627e+00,  2.79181480e+00, -3.40042877e+00, -2.85157710e-02,\n",
       "       -4.67910059e-02, -3.28744364e+00,  1.14483938e-01, -6.35292947e-01,\n",
       "        8.82890642e-01,  2.90657163e+00, -5.00349365e-02,  7.18983829e-01,\n",
       "       -1.94126415e+00,  5.35484076e+00, -1.28440881e+00,  3.77948856e+00,\n",
       "        6.78458405e+00, -1.16081655e+00, -2.74959296e-01, -2.21529269e+00,\n",
       "        1.06838322e+00,  5.53267050e+00,  5.53158140e+00,  1.81699693e+00,\n",
       "        9.91465986e-01,  3.61916637e+00, -7.14929029e-02, -9.73328233e-01,\n",
       "        3.56985021e+00, -2.66915751e+00, -3.36940169e-01,  1.19163942e+00,\n",
       "        1.60958374e+00, -5.50196469e-01, -3.70190120e+00, -6.20832300e+00,\n",
       "        4.10945702e+00, -1.44344926e+00, -2.77774692e+00, -3.21367550e+00,\n",
       "        2.78067064e+00,  4.97559023e+00,  1.59511137e+00, -5.58946013e-01,\n",
       "       -5.41198015e-01,  3.57315683e+00,  5.34875870e-01, -1.11725485e+00,\n",
       "       -1.92330527e+00, -6.07355833e+00,  1.66732514e+00,  3.40882158e+00,\n",
       "        3.05100131e+00, -6.47869110e+00,  3.28676891e+00,  1.50513053e-01,\n",
       "        3.81144929e+00, -8.68763804e-01, -1.50011039e+00, -5.76293802e+00,\n",
       "       -8.06317627e-01,  1.04988682e+00,  2.02614141e+00,  6.35202304e-02,\n",
       "        1.47653735e+00, -2.07837343e+00,  4.86302972e-01, -2.82573891e+00,\n",
       "       -3.10036826e+00,  2.25327468e+00, -1.00570583e+00, -5.96572816e-01,\n",
       "       -2.52224064e+00, -1.87344182e+00, -1.38603842e+00, -6.67736471e-01,\n",
       "        4.91625881e+00,  1.41651618e+00, -6.38189840e+00,  1.16400254e+00],\n",
       "      dtype=float32)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_vec_dict['年']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "dict = {'字': 1, '成': 2}\n",
    "print(dict.get('迷'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0.])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "test1 = np.zeros((2,3))\n",
    "test1[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
